package com.baicaoyuan.web.controller;


import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.enums.ApplyStatusEnum;
import com.baicaoyuan.web.enums.ProjectStatusEnum;
import com.baicaoyuan.web.model.ProjectItemUserDO;
import com.baicaoyuan.web.request.BaseRequest;
import com.baicaoyuan.web.request.br.*;
import com.baicaoyuan.web.request.nbr.ProjectIdRequest;
import com.baicaoyuan.web.response.ProjectItemUserMsgResponse;
import com.baicaoyuan.web.response.ProjectItemUserResponse;
import com.baicaoyuan.web.response.ProjectResponse;
import com.baicaoyuan.web.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(value = "项目Controller", tags = {"项目操作接口"})
@RestController
@RequestMapping(value = "/project")
public class ProjectController {


    @Autowired
    ProjectService projectService;

    @ApiOperation(value = "后台-保存项目（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/saveProject", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status saveProject(@RequestBody BProjectRequest request) {
        try {
            projectService.saveProject(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-编辑项目（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/editProject", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status editProject(@RequestBody BProjectEditRequest request) {
        try {
            projectService.editProject(request);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-删除项目（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/deleteProject", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status deleteProject(@RequestBody BProjectIdRequest request) {
        try {
            projectService.updateProjectStatus(request.getId(), ProjectStatusEnum.DELETE);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-关闭项目（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/closeProject", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status closeProject(@RequestBody BProjectIdRequest request) {
        try {
            projectService.updateProjectStatus(request.getId(), ProjectStatusEnum.CLOSED);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "申请办理业务（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/applyProjectItem", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status applyProjectItem(@RequestBody BProjectItemApplyRequest request) {
        try {
            ProjectItemUserDO piu = projectService.applyProjectItem(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("id", piu.getId());
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "完善办理业务（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/perfectProjectItem", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status perfectProjectItem(@RequestBody BProjectItemApplyPerfectRequest request) {
        try {
            Objects.requireNonNull(request.getProjectItemUserId());
            boolean success = projectService.perfectProjectItem(request);
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", success);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED, ex.getMessage());
        }
    }


    @ApiOperation(value = "后台-审核业务申请（token）(jzz)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/checkProjectItemUser", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status checkProjectItemUser(@RequestBody BProjectItemUserCheckRequest request) {
        try {
            projectService.updateProjectItemUserStatus(request.getId(), request.getStatus(), request.getSerialNumber(), request.getRejectReason());
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED, ex.getMessage());
        }
    }

    @ApiOperation(value = "后台-删除业务申请（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/deleteProjectItemUser", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status deleteProjectItemUser(@RequestBody BProjectItemUserDeleteRequest request) {
        try {
            projectService.updateProjectItemUserStatus(request.getId(), ApplyStatusEnum.DELETED.getStatus(), null, null);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-获取业务申请列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getProjectItemUsers", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status getProjectItemUsers(@RequestBody BProjectIdRequest request) {
        try {
            List<ProjectItemUserResponse> list = this.projectService.getProjectItemUsers(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("projectItemUsers", list);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "获取一个用户的业务申请列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getProjectItemUsersByUser", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status getProjectItemUsersByUser(@RequestBody BaseRequest request) {
        try {
            List<ProjectItemUserResponse> list = this.projectService.getProjectItemUsersByUserId(request.getUserId());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("projectItemUsers", list);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-获取业务申请列表(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getTopProjectItemUsers", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status getTopProjectItemUsers(@RequestBody BTopRequest request) {
        try {
            List<ProjectItemUserMsgResponse> list = this.projectService.getTopProjectItemUsers(request.getTop());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("message", list);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "获取一个项目详情", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getProject", method = RequestMethod.POST)
    @ResponseBody
    public Status getProject(@RequestBody ProjectIdRequest request) {
        try {
            ProjectResponse projectResponse = projectService.getProject(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("project", projectResponse);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "获取项目列表", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getAllProjects", method = RequestMethod.GET)
    @ResponseBody
    public Status getAllProjects() {
        try {
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("projects", projectService.getProjects());
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "获取一个项目下的业务列表", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/getProjectItems", method = RequestMethod.POST)
    @ResponseBody
    public Status getProjectItems(@RequestBody BProjectIdRequest request) {
        try {
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("projectItems", projectService.getProjectItems(request.getId(), request.getUserId()));
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-删除业务（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/deleteProjectItem", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status deleteProjectItem(@RequestBody BProjectItemIdRequest request) {
        try {
            projectService.updateProjectItemStatus(request.getId(), ProjectStatusEnum.DELETE);
            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-判断是否已经申请信用卡并获得了积分（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/checkCardCanApply", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status checkCardCanApply(@RequestBody BProjectItemIdRequest request) {
        try {
            boolean canApply = !projectService.checkCardCanApply(request.getId(), request.getApplyUserId());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("canApply", canApply);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-增加业务（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/addProjectItem", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status addProjectItem(@RequestBody BProjectItemRequest request) {
        try {
            projectService.saveProjectItem(request);

            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-编辑业务（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/editProjectItem", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status editProjectItem(@RequestBody BProjectItemEditRequest request) {
        try {
            projectService.editProjectItem(request);

            Status result = new Status(StateMessage.SUCCESS);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }


    @ApiOperation(value = "后台-设置加入金种子项目（token）(jzz)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/joinJzz", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status joinJzz(@RequestBody BProjectIdRequest request) {
        try {
            boolean r = projectService.joinJzz(request.getId());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", r);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }
}
